import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Test from '../views/DevTest.vue'
import Login from '../views/Login.vue'
import Manager from '../views/manager/ManagerIndex.vue'
import Student from '../views/student/StudentIndex.vue'
import Teacher from '../views/teacher/TeacherIndex.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/morenews',
    name: 'MoreNews',
    component: () => import('../components/MoreNews.vue')
  },
  {
    path: '/morecourse',
    name: 'MoreCourse',
    component: () => import('../components/MoreCourse.vue')
  },
  {
    path: '/moreteacher',
    name: 'MoreTeacher',
    component: () => import('../components/MoreTeacher.vue')
  },
  {
    path: '/morestudent',
    name: 'MoreStudent',
    component: () => import('../components/MoreStudent.vue')
  },
  {
    path: '/course',
    name: 'Course',
    component: () => import('../components/Course.vue')
  },
  {
    path: '/teacherdetail',
    name: 'TeacherDetail',
    component: () => import('../components/TeacherDetail.vue')
  },
  {
    path: '/studentdetail',
    name: 'StudentDetail',
    component: () => import('../components/StudentDetail.vue')
  },
  {
    path: '/newsdetail',
    name: 'NewsDetail',
    component: () => import('../components/NewsDetail.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/test',
    name: Test,
    component: Test
  },
  {
    path: '/manager',
    name: 'Manager',
    redirect: '/manager/welcome',
    component: Manager,
    children: [{
      path: '/manager/welcome',
      component: () => import('../views/manager/Welcome.vue')
    }, {
      path: '/manager/course',
      component: () => import('../views/manager/course/Course.vue')
    }, {
      path: '/manager/setcourse',
      component: () => import('../views/manager/course/SetCourse.vue')
    }, {
      path: '/manager/checkcourse',
      component: () => import('../views/manager/course/CheckCourse.vue')
    }, {
      path: '/manager/exam',
      component: () => import('../views/manager/exam/Exam.vue')
    }, {
      path: '/manager/system/dic',
      component: () => import('../views/manager/system/Dic.vue')
    }, {
      path: '/manager/system/user',
      component: () => import('../views/manager/system/User.vue')
    }, {
      path: '/manager/msg/honor',
      component: () => import('../views/manager/msg/Honor.vue')
    }, {
      path: '/manager/msg/news',
      component: () => import('../views/manager/msg/News.vue')
    }, {
      path: '/manager/msg/person',
      component: () => import('../views/manager/msg/Person.vue')
    }]
  },
  {
    path: '/student',
    name: 'Student',
    redirect: '/student/studenthomepage',
    component: Student,
    children: [{
      path: '/student/studenthomepage',
      component: () => import('../views/student/StudentHomePage.vue')
    }, {
      path: '/student/selectcourse',
      redirect: '/student/selectcoursecenter',
      component: () => import('../views/student/StudentSubpage/SelectCourse.vue'),
      children: [{
        path: '/student/selectcoursecenter',
        component: () => import('../views/student/StudentSubpage/SelectCourseSubPage/SelectCourseCenter.vue')
      }, {
        path: '/student/selectdetail',
        component: () => import('../views/student/StudentSubpage/SelectCourseSubPage/SelectDetailTable.vue')
      }, {
        path: '/student/selectcoursemainpage',
        component: () => import('../views/student/StudentSubpage/SelectCourseSubPage/SelectCourseMainPage.vue')
      }]
    }, {
      path: '/student/myscore',
      component: () => import('../views/student/StudentSubpage/MyScore.vue')
    }, {
      path: '/student/schoolmanagement',
      component: () => import('../views/student/StudentSubpage/SchoolManagement.vue')
    }, {
      path: '/student/graduationmanagement',
      component: () => import('../views/student/StudentSubpage/GraduationManagement.vue')
    }, {
      path: '/student/mycoursetable',
      component: () => import('../views/student/StudentSubpage/MyCourseTable.vue')
    }, {
      path: '/student/myexam',
      component: () => import('../views/student/StudentSubpage/MyExam.vue')
    }, {
      path: '/student/checkcourse',
      component: () => import('../views/student/StudentSubpage/CheckCourse.vue')
    }, {
      path: '/student/editpersoninfo',
      component: () => import('../views/student/StudentSubpage/EditPersoninfo.vue')
    }]
  },
  {
    path: '/teacher',
    name: 'Teacher',
    redirect: '/teacher/teacherhomepage',
    component: Teacher,
    children: [{
      path: '/teacher/teacherhomepage',
      component: () => import('../views/teacher/TeacherHomePage.vue')
    }, {
      path: '/teacher/setscoreentry',
      component: () => import('../views/teacher/setScore/SetScoreEntry.vue')
    }, {
      path: '/teacher/setscore',
      component: () => import('../views/teacher/setScore/SetScore.vue')
    }, {
      path: '/teacher/checkscore',
      component: () => import('../views/teacher/setScore/CheckScore.vue')
    }, {
      path: '/teacher/inspectexam',
      component: () => import('../views/teacher/inspectExam/Index.vue')
    }, {
      path: '/teacher/coursemanage',
      redirect: '/teacher/mycourse',
      component: () => import('../views/teacher/courseManage/Index.vue'),
      children: [{
        path: '/teacher/mycourse',
        component: () => import('../views/teacher/courseManage/MyCourse.vue')
      }, {
        path: '/teacher/checkcourse',
        component: () => import('../views/teacher/courseManage/CheckCourse.vue')
      }, {
        path: '/teacher/editpersoninfo',
        component: () => import('../views/teacher/courseManage/EditPersoninfo.vue')
      }]
    }, {
      path: '/teacher/analysescore',
      component: () => import('../views/teacher/analyseScore/Index.vue'),
    }, {
      path: '/teacher/scoredetail',
      component: () => import('../views/teacher/analyseScore/Detail.vue')
    }]
  }
]

const router = new VueRouter({
  routes
})

// 设置导航守卫
router.beforeEach((to, from, next) => {
  // 先设置几个不需要token的页面
  if (to.path == '/login') {
    return next()
  } else if (to.path == '/home') {
    return next()
  } else if (to.path == '/morenews') {
    return next()
  } else if (to.path == '/morecourse') {
    return next()
  } else if (to.path == '/moreteacher') {
    return next()
  } else if (to.path == '/morestudent') {
    return next()
  } else if (to.path == '/course') {
    return next()
  } else if (to.path == '/newsdetail') {
    return next()
  } else if (to.path == '/teacherdetail') {
    return next()
  } else if (to.path == '/studentdetail') {
    return next()
  } else if (to.path == '/test') {
    return next()
  }

  // get token first
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
